using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

using GonzalesCore.CoreElements;

using Kypris2.CoreElements;

using Jamila2.CoreElements;
using Jamila2.Database;
using Jamila2.Tools;

namespace GonzalesCore.CoreManagers
{
    public enum LogInterface { System, User };

    public class LogManager : SystemLogger, IManager, IDisposable
    {
        private GonzalesDataStructure DataStructure = new GonzalesDataStructure();
        
        public bool IsReady = false;
        public string Conn = "";

        public SQLFactory CurSQLFactory = null;
        public TypeOfDatabase DBType = TypeOfDatabase.MSSQL;

        private string ErrMsg = "";
        public virtual string ErrorMessage
        {
            get
            {
                return this.ErrMsg;
            }
        }

        public LogManager(Jamila2.Database.TypeOfDatabase DBType, string Conn)
            : base(DBType, Conn)
        {
            this.DBType = DBType;
            this.Conn = Conn;
        }

        public LogManager(PersonalSessionAccount PrivateConfig)
            : base(PrivateConfig)
        {
            this.DBType = PrivateConfig.DatabaseType;
            this.Conn = PrivateConfig.DatabaseConnectionString;
        }

        #region Display

        #region GetActionLogByClaimID

        public LogsCollection GetActionLogByClaimID(string InternalID)
        {
            LogsCollection ResultCollection = null;

            if (!this.IsReady)
            {
                this.CurDBEngine = new DatabaseEngine(this.DBType, this.Conn);

                if (this.IsReady = this.CurDBEngine.Connect())
                {
                    this.CurSQLFactory = new SQLFactory(this.DBType);
                }

            }

            if (this.IsReady)
            {
                DatabaseParameters KeyParameters = new DatabaseParameters();
                //KeyParameters.Add(new DatabaseParameter(this.DataStructure.Views.ActionHistory.ClaimID.ActualFieldName, InternalID));
                KeyParameters.Add(new DatabaseParameter(this.DataStructure.StoredProcedures.ActionHistory.Param_InternalID.ActualFieldName, InternalID));

                this.CurSQLFactory.ExecuteStoredProcedure(this.DataStructure.StoredProcedures.ActionHistory.ActualTableName, KeyParameters);

                DataTable ResultTable = this.CurDBEngine.SelectQuery(this.CurSQLFactory.SQL);
                if (ResultTable != null)
                {
                    ResultCollection = new LogsCollection();

                    DataRow[] ResultRows = ResultTable.Select("", this.DataStructure.Views.ActionHistory.ActionDateTime.ActualFieldName);

                    foreach (DataRow ResultRow in ResultRows)
                    {
                        ActionLogItem CurLogItem = new ActionLogItem();
                        CurLogItem.ClaimInternalID = ResultRow[this.DataStructure.Views.ActionHistory.ClaimID.ActualFieldName].ToString();
                        CurLogItem.LogDate = Convert.ToDateTime(ResultRow[this.DataStructure.Views.ActionHistory.ActionDateTime.ActualFieldName]);
                        CurLogItem.LoggedBy = new SystemUser(
                            ResultRow[this.DataStructure.Views.ActionHistory.PersonnelID.ActualFieldName].ToString(),
                            ResultRow[this.DataStructure.Views.ActionHistory.EmployeeDisplayName.ActualFieldName].ToString());
                        CurLogItem.LoggedBy.EmailID = ResultRow[this.DataStructure.Views.ActionHistory.EmailID.ActualFieldName].ToString();
                        CurLogItem.LoggedBy.Email = ResultRow[this.DataStructure.Views.ActionHistory.Email.ActualFieldName].ToString();
                        CurLogItem.LoggedBy.EmployeeID = ResultRow[this.DataStructure.Views.ActionHistory.EmployeeID.ActualFieldName].ToString();
                        CurLogItem.ActionStatus = ClaimManager.GetClaimStatusFromCode(ResultRow[this.DataStructure.Views.ActionHistory.ActionStatus.ActualFieldName].ToString());
                        CurLogItem.ActionDescription = ResultRow[this.DataStructure.Views.ActionHistory.Action.ActualFieldName].ToString();

                        ResultCollection.Add(CurLogItem);
                    }
                }
                else
                {
                    this.ErrMsg = "[LogManager.GetActionLogByClaimID] : Failed at this.CurDBEngine.SelectQuery('" + this.CurSQLFactory.SQL + "') : " + this.CurDBEngine.ErrorMessage;
                }
            }

            return ResultCollection;
        }

        #endregion

        #region GetActionLogByClaimIDClaimAction

        public LogsCollection GetActionLogByClaimIDClaimAction(string InternalID, string ClaimAction)
        {
            LogsCollection ResultCollection = null;

            if (!this.IsReady)
            {
                this.CurDBEngine = new DatabaseEngine(this.DBType, this.Conn);

                if (this.IsReady = this.CurDBEngine.Connect())
                {
                    this.CurSQLFactory = new SQLFactory(this.DBType);
                }

            }

            if (this.IsReady)
            {
                DatabaseParameters KeyParameters = new DatabaseParameters();
                //KeyParameters.Add(new DatabaseParameter(this.DataStructure.Views.ActionHistory.ClaimID.ActualFieldName, InternalID));
                KeyParameters.Add(new DatabaseParameter(this.DataStructure.StoredProcedures.ActionHistory.Param_InternalID.ActualFieldName, InternalID));

                this.CurSQLFactory.ExecuteStoredProcedure(this.DataStructure.StoredProcedures.ActionHistory.ActualTableName, KeyParameters);

                DataTable ResultTable = this.CurDBEngine.SelectQuery(this.CurSQLFactory.SQL);
                if (ResultTable != null)
                {
                    ResultCollection = new LogsCollection();

                    DataRow[] ResultRows = ResultTable.Select("", this.DataStructure.Views.ActionHistory.ActionDateTime.ActualFieldName);

                    foreach (DataRow ResultRow in ResultRows)
                    {
                        if (ResultRow[this.DataStructure.Views.ActionHistory.Action.ActualFieldName].ToString().CompareTo(ClaimAction) == 0)
                        {
                            ActionLogItem CurLogItem = new ActionLogItem();
                            CurLogItem.ClaimInternalID = ResultRow[this.DataStructure.Views.ActionHistory.ClaimID.ActualFieldName].ToString();
                            CurLogItem.LogDate = Convert.ToDateTime(ResultRow[this.DataStructure.Views.ActionHistory.ActionDateTime.ActualFieldName]);
                            CurLogItem.LoggedBy = new SystemUser(
                                ResultRow[this.DataStructure.Views.ActionHistory.PersonnelID.ActualFieldName].ToString(),
                                ResultRow[this.DataStructure.Views.ActionHistory.EmployeeDisplayName.ActualFieldName].ToString());
                            CurLogItem.LoggedBy.EmailID = ResultRow[this.DataStructure.Views.ActionHistory.EmailID.ActualFieldName].ToString();
                            CurLogItem.LoggedBy.Email = ResultRow[this.DataStructure.Views.ActionHistory.Email.ActualFieldName].ToString();
                            CurLogItem.LoggedBy.EmployeeID = ResultRow[this.DataStructure.Views.ActionHistory.EmployeeID.ActualFieldName].ToString();
                            CurLogItem.ActionStatus = ClaimManager.GetClaimStatusFromCode(ResultRow[this.DataStructure.Views.ActionHistory.ActionStatus.ActualFieldName].ToString());
                            CurLogItem.ActionDescription = ResultRow[this.DataStructure.Views.ActionHistory.Action.ActualFieldName].ToString();

                            ResultCollection.Add(CurLogItem);
                        }
                    }
                }
                else
                {
                    this.ErrMsg = "[LogManager.GetActionLogByClaimID] : Failed at this.CurDBEngine.SelectQuery('" + this.CurSQLFactory.SQL + "') : " + this.CurDBEngine.ErrorMessage;
                }
            }

            return ResultCollection;
        }

        #endregion

        #region GetClaimActionLogCountByClaimID

        public int GetClaimActionLogCountByClaimID(string ClaimInternalID)
        {
            int Count = 0;

            if (!this.IsReady)
            {
                this.CurDBEngine = new DatabaseEngine(this.DBType, this.Conn);

                if (this.IsReady = this.CurDBEngine.Connect())
                {
                    this.CurSQLFactory = new SQLFactory(this.DBType);
                }

            }

            if (this.IsReady)
            {
                DatabaseParameters KeyParameters = new DatabaseParameters();
                KeyParameters.Add(new DatabaseParameter(this.DataStructure.Tables.LogClaimAction.ClaimInternalID.ActualFieldName, ClaimInternalID));

                this.CurSQLFactory.SelectCommand(KeyParameters, this.DataStructure.Tables.LogClaimAction.ActualTableName);

                DataTable ResultTable = this.CurDBEngine.SelectQuery(this.CurSQLFactory.SQL);
                if (ResultTable != null)
                {
                    if (ResultTable.Rows.Count > 0)
                    {
                        if (ResultTable.Rows[0][1].ToString().Length > 0)
                            Count = 1;
                        else if (ResultTable.Rows[0][5].ToString().Length > 0)
                            Count = 2;
                        else if (ResultTable.Rows[0][9].ToString().Length > 0)
                            Count = 3;
                        else if (ResultTable.Rows[0][13].ToString().Length > 0)
                            Count = 4;
                        else if (ResultTable.Rows[0][17].ToString().Length > 0)
                            Count = 5;
                        else if (ResultTable.Rows[0][21].ToString().Length > 0)
                            Count = 6;
                    }
                    else
                    {
                        Count = 0;
                    }
                }
                else
                {
                    this.ErrMsg = "[LogManager.GetClaimActionLogCountByClaimID] : Failed at this.CurDBEngine.SelectQuery('" + this.CurSQLFactory.SQL + "') : " + this.CurDBEngine.ErrorMessage;
                }
            }

            return Count;
        }

        #endregion

        #region GetAllErrorLog

        public LogsCollection GetAllErrorLog()
        {
            LogsCollection ResultCollection = null;

            if (!this.IsReady)
            {
                this.CurDBEngine = new DatabaseEngine(this.DBType, this.Conn);

                if (this.IsReady = this.CurDBEngine.Connect())
                {
                    this.CurSQLFactory = new SQLFactory(this.DBType);
                }

            }

            if (this.IsReady)
            {
                DatabaseParameters KeyParameters = new DatabaseParameters();

                this.CurSQLFactory.SelectCommand(KeyParameters, this.DataStructure.Views.ErrorHistory.ActualTableName);

                DataTable ResultTable = this.CurDBEngine.SelectQuery(this.CurSQLFactory.SQL);
                if (ResultTable != null)
                {
                    ResultCollection = new LogsCollection();

                    DataRow[] ResultRows = ResultTable.Select("", this.DataStructure.Views.ErrorHistory.LogDateTime.ActualFieldName);

                    foreach (DataRow ResultRow in ResultRows)
                    {
                        ErrorLogItem CurLogItem = new ErrorLogItem();

                        CurLogItem.LogID = ResultRow[this.DataStructure.Views.ErrorHistory.LogID.ActualFieldName].ToString();
                        CurLogItem.LogDate = Convert.ToDateTime(ResultRow[this.DataStructure.Views.ErrorHistory.LogDateTime.ActualFieldName]);
                        CurLogItem.LoggedBy = new SystemUser(
                            ResultRow[this.DataStructure.Views.ErrorHistory.EmployeeInternalID.ActualFieldName].ToString(),
                            ResultRow[this.DataStructure.Views.ErrorHistory.EmployeeDisplayName.ActualFieldName].ToString());
                        CurLogItem.LoggedBy.EmailID = ResultRow[this.DataStructure.Views.ErrorHistory.EmailID.ActualFieldName].ToString();
                        CurLogItem.LoggedBy.Email = ResultRow[this.DataStructure.Views.ErrorHistory.Email.ActualFieldName].ToString();
                        CurLogItem.LoggedBy.EmployeeID = ResultRow[this.DataStructure.Views.ErrorHistory.EmployeeID.ActualFieldName].ToString();

                        CurLogItem.Description = ResultRow[this.DataStructure.Views.ErrorHistory.Description.ActualFieldName].ToString();

                        ResultCollection.Add(CurLogItem);
                    }
                }
                else
                {
                    this.ErrMsg = "[LogManager.GetActionLogByClaimID] : Failed at this.CurDBEngine.SelectQuery('" + this.CurSQLFactory.SQL + "') : " + this.CurDBEngine.ErrorMessage;
                }
            }

            return ResultCollection;
        }

        #endregion

        #region GetAllGeneralLog

        public LogsCollection GetAllGeneralLog()
        {
            LogsCollection ResultCollection = null;

            if (!this.IsReady)
            {
                this.CurDBEngine = new DatabaseEngine(this.DBType, this.Conn);

                if (this.IsReady = this.CurDBEngine.Connect())
                {
                    this.CurSQLFactory = new SQLFactory(this.DBType);
                }

            }

            if (this.IsReady)
            {
                DatabaseParameters KeyParameters = new DatabaseParameters();

                this.CurSQLFactory.SelectCommand(KeyParameters, this.DataStructure.Views.GeneralActionHistory.ActualTableName);

                DataTable ResultTable = this.CurDBEngine.SelectQuery(this.CurSQLFactory.SQL);
                if (ResultTable != null)
                {
                    ResultCollection = new LogsCollection();

                    DataRow[] ResultRows = ResultTable.Select("", this.DataStructure.Views.GeneralActionHistory.LogDateTime.ActualFieldName);

                    foreach (DataRow ResultRow in ResultRows)
                    {
                        GeneralActionLogItem CurLogItem = new GeneralActionLogItem();
                        
                        CurLogItem.LogDate = Convert.ToDateTime(ResultRow[this.DataStructure.Views.GeneralActionHistory.LogDateTime.ActualFieldName]);
                        CurLogItem.LoggedBy = new SystemUser(
                            ResultRow[this.DataStructure.Views.GeneralActionHistory.EmployeeInternalID.ActualFieldName].ToString(),
                            ResultRow[this.DataStructure.Views.GeneralActionHistory.EmployeeDisplayName.ActualFieldName].ToString());
                        CurLogItem.LoggedBy.EmailID = ResultRow[this.DataStructure.Views.GeneralActionHistory.EmailID.ActualFieldName].ToString();
                        CurLogItem.LoggedBy.Email = ResultRow[this.DataStructure.Views.GeneralActionHistory.Email.ActualFieldName].ToString();
                        CurLogItem.LoggedBy.EmployeeID = ResultRow[this.DataStructure.Views.GeneralActionHistory.EmployeeID.ActualFieldName].ToString();

                        CurLogItem.Description = ResultRow[this.DataStructure.Views.GeneralActionHistory.Description.ActualFieldName].ToString();

                        ResultCollection.Add(CurLogItem);
                    }
                }
                else
                {
                    this.ErrMsg = "[LogManager.GetAllGeneralLog] : Failed at this.CurDBEngine.SelectQuery('" + this.CurSQLFactory.SQL + "') : " + this.CurDBEngine.ErrorMessage;
                }
            }

            return ResultCollection;
        }

        #endregion

        #endregion

        #region Actions

        #region LogMessage
        public bool LogMessage(string Content, SystemUser CurrentUser, LogInterface Interface)
        {
            bool smooth = false;

            if (!this.IsReady)
            {
                this.CurDBEngine = new DatabaseEngine(this.DBType, this.Conn);

                if (this.IsReady = this.CurDBEngine.Connect())
                {
                    this.CurSQLFactory = new SQLFactory(this.DBType);
                }

            }

            if (this.IsReady)
            {

                DatabaseParameters Params = new DatabaseParameters();
                Params.Add(new DatabaseParameter(DataStructure.Tables.ActionsLog.LogDateTime.ActualFieldName, DateTime.Now));
                Params.Add(new DatabaseParameter(DataStructure.Tables.ActionsLog.Interface.ActualFieldName, Interface.ToString()));
                Params.Add(new DatabaseParameter(DataStructure.Tables.ActionsLog.Description.ActualFieldName, Content.Replace("'", "''"), true));
                Params.Add(new DatabaseParameter(DataStructure.Tables.ActionsLog.LogBy.ActualFieldName, CurrentUser.InternalID));

                this.CurSQLFactory.InsertCommand(Params, DataStructure.Tables.ActionsLog.ActualTableName);
                string sql = this.CurSQLFactory.SQL;

                try
                {
                    this.CurDBEngine.ExecuteQuery(sql);
                    smooth = true;
                }
                catch (Exception ex)
                {
                    smooth = false;
                    this.ErrMsg = ex.Message;
                }

            }

            return smooth;
        }
        #endregion

        #region LogError
        public bool LogError(string LogID, string Content, SystemUser CurrentUser, LogInterface Interface)
        {
            bool smooth = false;

            if (!this.IsReady)
            {
                this.CurDBEngine = new DatabaseEngine(this.DBType, this.Conn);

                if (this.IsReady = this.CurDBEngine.Connect())
                {
                    this.CurSQLFactory = new SQLFactory(this.DBType);
                }

            }

            if (this.IsReady)
            {

                DatabaseParameters Params = new DatabaseParameters();
                Params.Add(new DatabaseParameter(DataStructure.Tables.ErrorLog.LogID.ActualFieldName, LogID));
                Params.Add(new DatabaseParameter(DataStructure.Tables.ErrorLog.LogDateTime.ActualFieldName, DateTime.Now));
                Params.Add(new DatabaseParameter(DataStructure.Tables.ActionsLog.Interface.ActualFieldName, Interface.ToString()));
                Params.Add(new DatabaseParameter(DataStructure.Tables.ErrorLog.Description.ActualFieldName, Content.Replace("'", "''"), true));
                Params.Add(new DatabaseParameter(DataStructure.Tables.ErrorLog.LogBy.ActualFieldName, CurrentUser.InternalID));

                this.CurSQLFactory.InsertCommand(Params, DataStructure.Tables.ErrorLog.ActualTableName);
                string sql = this.CurSQLFactory.SQL;

                try
                {
                    this.CurDBEngine.ExecuteQuery(sql);
                    smooth = true;
                }
                catch (Exception ex)
                {
                    smooth = false;
                    this.ErrMsg = ex.Message;
                }

            }

            return smooth;
        }

        public bool LogError(string Content, SystemUser CurrentUser, LogInterface Interface)
        {
            return this.LogError(SwissArmy.UniqueID(), Content, CurrentUser, Interface);
        }
        #endregion

        #region LogAction
        public bool LogAction(string ClaimID, string UserID, string Action, GonzalesClaimHeader.TypeOfClaimStatus ActionStatus)
        {
            bool smooth = false;

            if (!this.IsReady)
            {
                this.CurDBEngine = new DatabaseEngine(this.DBType, this.Conn);

                if (this.IsReady = this.CurDBEngine.Connect())
                {
                    this.CurSQLFactory = new SQLFactory(this.DBType);
                }

            }

            if (this.IsReady)
            {

                DatabaseParameters Params = new DatabaseParameters();
                Params.Add(new DatabaseParameter(DataStructure.Tables.ActionHistory.ClaimID.ActualFieldName, ClaimID));
                Params.Add(new DatabaseParameter(DataStructure.Tables.ActionHistory.PersonnelID.ActualFieldName, UserID));
                Params.Add(new DatabaseParameter(DataStructure.Tables.ActionHistory.Action.ActualFieldName, Action));
                Params.Add(new DatabaseParameter(DataStructure.Tables.ActionHistory.ActionDateTime.ActualFieldName, DateTime.Now));
                Params.Add(new DatabaseParameter(DataStructure.Tables.ActionHistory.ActionStatus.ActualFieldName, ClaimManager.GetClaimStatusInCode(ActionStatus).ToString() ));

                this.CurSQLFactory.InsertCommand(Params, DataStructure.Tables.ActionHistory.ActualTableName);
                string sql = this.CurSQLFactory.SQL;

                try
                {
                    this.CurDBEngine.ExecuteQuery(sql);
                    smooth = true;
                }
                catch (Exception ex)
                {
                    smooth = false;
                    this.ErrMsg = ex.Message;
                }

            }

            return smooth;
        }

        #endregion

        #region CreateLogApproverClaimAction
        public bool CreateLogApproverClaimAction(string ClaimID, string ApprInternalID, string ApprDisplayName, string ApprStatus)
        {
            bool smooth = false;

            if (!this.IsReady)
            {
                this.CurDBEngine = new DatabaseEngine(this.DBType, this.Conn);

                if (this.IsReady = this.CurDBEngine.Connect())
                {
                    this.CurSQLFactory = new SQLFactory(this.DBType);
                }

            }

            if (this.IsReady)
            {

                DatabaseParameters Params = new DatabaseParameters();
                Params.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.ClaimInternalID.ActualFieldName, ClaimID));
                Params.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.Appr1_InternalID.ActualFieldName, ApprInternalID));
                Params.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.Appr1_DisplayName.ActualFieldName, ApprDisplayName.Replace("'","''"),true, true));
                Params.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.Appr1_Status.ActualFieldName, ApprStatus));
                Params.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.Appr1_Date.ActualFieldName, DateTime.Now));
               
                this.CurSQLFactory.InsertCommand(Params, DataStructure.Tables.LogClaimAction.ActualTableName);
                string sql = this.CurSQLFactory.SQL;

                try
                {
                    this.CurDBEngine.ExecuteQuery(sql);
                    smooth = true;
                }
                catch (Exception ex)
                {
                    smooth = false;
                    this.ErrMsg = ex.Message;
                }

            }

            return smooth;
        }

        #endregion

        #region UpdateLogApproverClaimAction
        public bool UpdateLogApproverClaimAction(string ClaimID, string ApprInternalID, string ApprDisplayName, string ApprStatus, string ApprLevel)
        {
            bool smooth = false;

            if (!this.IsReady)
            {
                this.CurDBEngine = new DatabaseEngine(this.DBType, this.Conn);

                if (this.IsReady = this.CurDBEngine.Connect())
                {
                    this.CurSQLFactory = new SQLFactory(this.DBType);
                }

            }

            if (this.IsReady)
            {

                DatabaseParameters Params = new DatabaseParameters();
                DatabaseParameters keys = new DatabaseParameters();

                keys.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.ClaimInternalID.ActualFieldName, ClaimID));

                if (ApprLevel.CompareTo("1") == 0)
                {
                    Params.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.Appr2_InternalID.ActualFieldName, ApprInternalID));
                    Params.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.Appr2_DisplayName.ActualFieldName, ApprDisplayName.Replace("'","''"),true, true));
                    Params.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.Appr2_Status.ActualFieldName, ApprStatus));
                    Params.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.Appr2_Date.ActualFieldName, DateTime.Now));
                }
                else if (ApprLevel.CompareTo("2") == 0)
                {
                    Params.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.Appr3_InternalID.ActualFieldName, ApprInternalID));
                    Params.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.Appr3_DisplayName.ActualFieldName, ApprDisplayName.Replace("'","''"), true, true));
                    Params.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.Appr3_Status.ActualFieldName, ApprStatus));
                    Params.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.Appr3_Date.ActualFieldName, DateTime.Now));
                }
                else if (ApprLevel.CompareTo("3") == 0)
                {
                    Params.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.Appr4_InternalID.ActualFieldName, ApprInternalID));
                    Params.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.Appr4_DisplayName.ActualFieldName, ApprDisplayName.Replace("'","''"), true, true));
                    Params.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.Appr4_Status.ActualFieldName, ApprStatus));
                    Params.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.Appr4_Date.ActualFieldName, DateTime.Now));
                }
                else if (ApprLevel.CompareTo("4") == 0)
                {
                    Params.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.Appr5_InternalID.ActualFieldName, ApprInternalID));
                    Params.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.Appr5_DisplayName.ActualFieldName, ApprDisplayName.Replace("'", "''"), true, true));
                    Params.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.Appr5_Status.ActualFieldName, ApprStatus));
                    Params.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.Appr5_Date.ActualFieldName, DateTime.Now));
                }
                else if (ApprLevel.CompareTo("5") == 0)
                {
                    Params.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.Appr6_InternalID.ActualFieldName, ApprInternalID));
                    Params.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.Appr6_DisplayName.ActualFieldName, ApprDisplayName.Replace("'", "''"), true, true));
                    Params.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.Appr6_Status.ActualFieldName, ApprStatus));
                    Params.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.Appr6_Date.ActualFieldName, DateTime.Now));
                }
                else if (ApprLevel.CompareTo("6") == 0)
                {
                    Params.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.Appr7_InternalID.ActualFieldName, ApprInternalID));
                    Params.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.Appr7_DisplayName.ActualFieldName, ApprDisplayName.Replace("'", "''"), true, true));
                    Params.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.Appr7_Status.ActualFieldName, ApprStatus));
                    Params.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.Appr7_Date.ActualFieldName, DateTime.Now));
                }


                this.CurSQLFactory.UpdateCommand(keys, Params, DataStructure.Tables.LogClaimAction.ActualTableName);
                string sql = this.CurSQLFactory.SQL;

                try
                {
                    this.CurDBEngine.ExecuteQuery(sql);
                    smooth = true;
                }
                catch (Exception ex)
                {
                    smooth = false;
                    this.ErrMsg = ex.Message;
                }

            }

            return smooth;
        }

        #endregion

        #region UpdateLogAuthorizerClaimAction
        public bool UpdateLogAuthorizerClaimAction(string ClaimID, string AuthorizerInternalID, string AuthorizerDisplayName, string AuthorizerStatus)
        {
            bool smooth = false;

            if (!this.IsReady)
            {
                this.CurDBEngine = new DatabaseEngine(this.DBType, this.Conn);

                if (this.IsReady = this.CurDBEngine.Connect())
                {
                    this.CurSQLFactory = new SQLFactory(this.DBType);
                }

            }

            if (this.IsReady)
            {

                DatabaseParameters Params = new DatabaseParameters();
                DatabaseParameters keys = new DatabaseParameters();

                keys.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.ClaimInternalID.ActualFieldName, ClaimID));
                Params.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.Authorizer_InternalID.ActualFieldName, AuthorizerInternalID));
                Params.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.Authorizer_DisplayName.ActualFieldName, AuthorizerDisplayName.Replace("'", "''"), true, true));
                Params.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.Authorizer_Status.ActualFieldName, AuthorizerStatus));
                Params.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.Authorizer_Date.ActualFieldName, DateTime.Now));

               

                this.CurSQLFactory.UpdateCommand(keys, Params, DataStructure.Tables.LogClaimAction.ActualTableName);
                string sql = this.CurSQLFactory.SQL;

                try
                {
                    this.CurDBEngine.ExecuteQuery(sql);
                    smooth = true;
                }
                catch (Exception ex)
                {
                    smooth = false;
                    this.ErrMsg = ex.Message;
                }

            }

            return smooth;
        }

        #endregion

        #region UpdateLogFAClaimAction
        public bool UpdateLogFAClaimAction(string ClaimID, string FAInternalID, string FADisplayName, string FAStatus)
        {
            bool smooth = false;

            if (!this.IsReady)
            {
                this.CurDBEngine = new DatabaseEngine(this.DBType, this.Conn);

                if (this.IsReady = this.CurDBEngine.Connect())
                {
                    this.CurSQLFactory = new SQLFactory(this.DBType);
                }

            }

            if (this.IsReady)
            {

                DatabaseParameters Params = new DatabaseParameters();
                DatabaseParameters keys = new DatabaseParameters();

                keys.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.ClaimInternalID.ActualFieldName, ClaimID));
                Params.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.fa_InternalID.ActualFieldName, FAInternalID));
                Params.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.fa_DisplayName.ActualFieldName, FADisplayName.Replace("'", "''"), true, true));
                Params.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.fa_Status.ActualFieldName, FAStatus));
                Params.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.fa_Date.ActualFieldName, DateTime.Now));



                this.CurSQLFactory.UpdateCommand(keys, Params, DataStructure.Tables.LogClaimAction.ActualTableName);
                string sql = this.CurSQLFactory.SQL;

                try
                {
                    this.CurDBEngine.ExecuteQuery(sql);
                    smooth = true;
                }
                catch (Exception ex)
                {
                    smooth = false;
                    this.ErrMsg = ex.Message;
                }

            }

            return smooth;
        }

        #endregion

        #region UpdateLogFVClaimAction
        public bool UpdateLogFVClaimAction(string ClaimID, string FVInternalID, string FVDisplayName, string FVStatus)
        {
            bool smooth = false;

            if (!this.IsReady)
            {
                this.CurDBEngine = new DatabaseEngine(this.DBType, this.Conn);

                if (this.IsReady = this.CurDBEngine.Connect())
                {
                    this.CurSQLFactory = new SQLFactory(this.DBType);
                }

            }

            if (this.IsReady)
            {

                DatabaseParameters Params = new DatabaseParameters();
                DatabaseParameters keys = new DatabaseParameters();

                keys.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.ClaimInternalID.ActualFieldName, ClaimID));
                Params.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.fv_InternalID.ActualFieldName, FVInternalID));
                Params.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.fv_DisplayName.ActualFieldName, FVDisplayName.Replace("'", "''"), true, true));
                Params.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.fv_Status.ActualFieldName, FVStatus));
                Params.Add(new DatabaseParameter(DataStructure.Tables.LogClaimAction.fv_Date.ActualFieldName, DateTime.Now));



                this.CurSQLFactory.UpdateCommand(keys, Params, DataStructure.Tables.LogClaimAction.ActualTableName);
                string sql = this.CurSQLFactory.SQL;

                try
                {
                    this.CurDBEngine.ExecuteQuery(sql);
                    smooth = true;
                }
                catch (Exception ex)
                {
                    smooth = false;
                    this.ErrMsg = ex.Message;
                }

            }

            return smooth;
        }

        #endregion

        #region ClearErrorLog
        public bool ClearErrorLog()
        {
            bool smooth = false;

            if (!this.IsReady)
            {
                this.CurDBEngine = new DatabaseEngine(this.DBType, this.Conn);

                if (this.IsReady = this.CurDBEngine.Connect())
                {
                    this.CurSQLFactory = new SQLFactory(this.DBType);
                }

            }

            if (this.IsReady)
            {
                DatabaseParameters KeyParameters = new DatabaseParameters();

                this.CurSQLFactory.DeleteCommand(KeyParameters, this.DataStructure.Tables.ErrorLog.ActualTableName);

                try
                {
                    this.CurDBEngine.ExecuteQuery(this.CurSQLFactory.SQL);
                    smooth = true;
                }
                catch (Exception ex)
                {
                    smooth = false;
                    this.ErrMsg = ex.Message;
                }

            }
            return smooth;
        }
        #endregion

        #region ClearGeneralActionLog
        public bool ClearGeneralActionLog()
        {
            bool smooth = false;

            if (!this.IsReady)
            {
                this.CurDBEngine = new DatabaseEngine(this.DBType, this.Conn);

                if (this.IsReady = this.CurDBEngine.Connect())
                {
                    this.CurSQLFactory = new SQLFactory(this.DBType);
                }

            }

            if (this.IsReady)
            {
                DatabaseParameters KeyParameters = new DatabaseParameters();

                this.CurSQLFactory.DeleteCommand(KeyParameters, this.DataStructure.Tables.ActionsLog.ActualTableName);

                try
                {
                    this.CurDBEngine.ExecuteQuery(this.CurSQLFactory.SQL);
                    smooth = true;
                }
                catch (Exception ex)
                {
                    smooth = false;
                    this.ErrMsg = ex.Message;
                }

            }
            return smooth;
        }
        #endregion

        #endregion

        public void Dispose()
        {
            this.CurDBEngine = null;
        }
    }
    
    
}
